<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\User;
use Validator;
use Cookie;
use App\Mail\Ordership;
use Illuminate\Support\Facades\Mail;

class UserController extends Controller
{
    /**获取用户
     * @author
     * @date
     */
    public function getUsers(){
        return response(
            [
                'data' => User::all()->toArray(),
                'code' => 200
                ]
        );
    }

    public function manageUser(Request $request){
        $data = $request->all();
        foreach($data['add']??[] as $index =>$item){
            User::create([
                'email'=>$item['email'],
                'password'=>$item['password']??123456,
                'root' => $item['root']??0,
            ]);
        }
        User::destroy(array_pluck($data['remove']??[],'email'));
        foreach($data['update']??[] as $index =>$item){
            User::find($item['email'])->update([
                'email' => $item['email'],
                'password' => $item['password']??123456,
                'root' => $item['root']??0,
            ]);
        }

        return response(
            [
                'msg' => '提交成功',
                'code' => 200
            ]
        );
    }

    /**
     * 管理员注册
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @author
     * @date xxx
     */
    public function register(Request $request) {
        $validator = Validator::make($request->all(),
            [
            'email' => 'required|email|unique:users,email',
            'password' => 'required|size:6',
            ]
            );
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $user = new User;
        $user -> uid = count($user->all()->toArray())+1000000;
        $user -> email = $request -> get('email');
        $user -> password = $request -> get('password');
        $user -> root = $request -> get('root');
        $user -> save();
        return response('添加成功',200);
    }
    /**
     * 客户注册
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @author
     * @date xxx
     */
    public function webregister(Request $request) {
        $session_email_code = session('email_code')??'';

        $validator = Validator::make($request->all(),
            [
                'email' => 'required|email|unique:users,email',
                'password' => 'required|size:6',
                'code' => [function($attribute, $value, $fail){
                    if($value != session('email_code')){
                        return $fail('验证码错误');
                    }
                }]
            ]
        );
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
//        $user = new User;
//        $user -> uid = count($user->all()->toArray())+1000000;
//        $user -> email = $request -> get('email');
//        $user -> password = $request -> get('password');
//        $user -> root = $request -> get('root')??0;
//        $user -> save();
        return response(['msg' =>'注册成功', 'code' => 200],200);
    }

    public function sendCode(Request $request){
        $validator = Validator::make($request->all(),
            [
                'email' => 'required|email|unique:users,email'
            ]
        );
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $code = rand(1000, 9999);
        session(['email_code'=>$code]);
        Mail::to([
            $request->email
        ])->send(new Ordership($code));
        if(isset(Mail::failures()[0])){
            return response('发送失败，请重试',500);
        }else{
            return response('发送成功',200);
        }
    }
    public function login(Request $request) {
        $validator = Validator::make($request->all(),
            [
                'email' => 'required|email|exists:users,email',
                'password' => 'required',
            ]
        );
        if ($validator->fails()) {
            return response(['msg'=> $validator->errors()->first(),'code' => 700]);
        }
        return User::find($request->email)->check($request);
    }

    public function logOut(Request $request) {
        $request->session()->forget('user');
        return response(['msg'=>'已退出登录','code'=>701],200);
    }
    public function getLoginStatus(Request $request){
        $user = session('user');
        if($user['token'] && Cookie::get('token') === $user['token']){
            return response(['msg'=>'已登录','code' => 200]);
        }else {
            return response(['msg'=>'未登录','code' => 702]);
        }
    }
}
